from django.shortcuts import render

# Create your views here.
from rest_framework import viewsets
from rest_framework.generics import CreateAPIView
from rest_framework.response import Response
from rest_framework.views import APIView


from users.models import User
from users.serializers import CreateUserSerializer


from django.contrib.auth.models import Group
from rest_framework.permissions import IsAuthenticated
from .serializers import UserGroupsSerializer


class UserGropInfoViewset(viewsets.ModelViewSet):
    permission_classes = (IsAuthenticated,)
    queryset = Group.objects.all()
    serializer_class = UserGroupsSerializer

# TODO 注册接口&注册序列化器待实现
class UserView(CreateAPIView):
    serializer_class = CreateUserSerializer


# GET usernames/(?P<username>\w{5,20})/count/
class UserNameCountView(APIView):
    """查询用户名是否存在"""
    def get(self, request, username):
        count = User.objects.filter(username=username).count()
        data = {
            'username': username,
            'count': count
        }
        return Response(data)


# GET mobiles/(?P<mobile>1[3456789]\d{9})/count/
class MobileCountView(APIView):
    """查询手机号否存在"""
    def get(self, request, mobile):
        count = User.objects.filter(mobile=mobile).count()
        data = {
            'mobile': mobile,
            'count': count
        }
        return Response(data)